package SocialMediaSentiment;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.StringTokenizer;

import SocialMediaSentiment.ReadExcel;

public class adaBoosting {
	public static void main(String[] args) throws IOException {
		String file = "CorrectedTweets.xls";
		calcAdaBoosting(file);
	}
	public static void calcAdaBoosting(String file) throws IOException
	{
		int kfold = 10;
		String tweetsFile = file;
		HashMap<Integer, HashMap<String, String>[]> sets = new HashMap<Integer, HashMap<String, String>[]>();
		HashMap<Integer, HashMap<String, Float>> wordCounters = new HashMap<Integer, HashMap<String, Float>>();
		HashMap<Integer, Integer> bestWordCounter = new HashMap<Integer, Integer>();
		for (int i = 1; i <= kfold; i++) {
			// create sets: {trainset, validationset}
			HashMap<String, String>[] trainVilisetsK = createSets(tweetsFile,
					i, kfold);
			sets.put(i, trainVilisetsK);
			// create counts: {zero, nonzero, all}
			HashMap<String, Float> wordCountsK = createWeights(trainVilisetsK[0]);
			wordCounters.put(i, wordCountsK);
		}
		for(int i = 1; i < kfold; i++)
		{
			float treshold = (float) 0.5;
			// {trainset, validationset}
			HashMap<String, String>[] setsK = sets.get(i);
			HashMap<String, String> trainset = setsK[0];
			HashMap<String, String> validationset = setsK[1];
			// {zero, nonzero, all}
			HashMap<String, Float> weightlist = wordCounters.get(i);
			Iterator<String> tweets = (Iterator<String>) trainset.keySet().iterator();
			while(tweets.hasNext())
			{
				String tweet = tweets.next();
				float tweetValue = 0;
				int sentiment = Integer.parseInt(trainset.get(tweet).replace(" ", ""));
				StringTokenizer words = new StringTokenizer(tweet,
						" \r\n\t\f,.!$%^&*()[]{}-=\\;<>/?");
				while (words.hasMoreTokens()) {	
					String word = words.nextToken();
					float weight;
					if(weightlist.containsKey(word))
					{
						weight = weightlist.get(word);
					}
					else
					{
						weight = 1/(weightlist.size()+1);
						weightlist.put(word, weight);
					}
					tweetValue += weight;
				}
				if(tweetValue > treshold);
			}
			
		}
	}

	
	public static HashMap<String, Float> createWeights(
			HashMap<String, String> trainSet) {
		HashMap[] counters = new HashMap[3];
		HashMap<String, Float> wordsCountAll = new HashMap<String, Float>();
		Iterator<String> k2 = (Iterator<String>) trainSet.keySet().iterator();
		while (k2.hasNext()) {
			String tweet = k2.next();
			StringTokenizer st = new StringTokenizer(tweet,
					" \r\n\t\f,.!$%^&*()[]{}-=\\;<>/?");
			while (st.hasMoreTokens()) {
					String key = st.nextToken();
					float value = 1;
					wordsCountAll.put(key, value);
			}
		}
		int m = wordsCountAll.size();
		Iterator<String> words = (Iterator<String>) wordsCountAll.keySet().iterator();
		while(words.hasNext())
		{
			String word = words.next();
			wordsCountAll.put(word, wordsCountAll.get(word)/m);
			System.out.println(wordsCountAll.get(word)/m);
		}
		return wordsCountAll;
	}
	
	public static HashMap<String, String>[] createSets(String alltweetsFile,
			int times, int kfold) throws IOException {

		HashMap<Integer, String[]> tweetsWithSentiment = ReadExcel.read(
				alltweetsFile, 0, 1);
		int maxindex = tweetsWithSentiment.size() - 1;
		Iterator<Integer> k = (Iterator<Integer>) tweetsWithSentiment.keySet()
				.iterator();
		HashMap<String, String> trainSet = new HashMap<String, String>();
		HashMap<String, String> validationSet = new HashMap<String, String>();

		while (k.hasNext()) {
			int index = k.next();
			String[] tweetSentiment = tweetsWithSentiment.get(index);

			if (index < (maxindex - (maxindex / kfold * times))
					|| index > (maxindex - (maxindex / kfold * (times - 1)))) {
				trainSet.put(tweetSentiment[0], tweetSentiment[1]);
			} else {
				validationSet.put(tweetSentiment[0], tweetSentiment[1]);
			}
		}
		HashMap[] kfoldSets = new HashMap[2];
		kfoldSets[0] = trainSet;
		kfoldSets[1] = validationSet;

		return kfoldSets;
	}
	
	
	
}
